/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.create;

import br.com.deployment.daoImpl.CategoryDAOImpl;
import br.com.deployment.daoImpl.QuestionnaireDAOImpl;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Questionnaire;
import br.com.deployment.enums.TypeIssues;
import br.com.deployment.utils.Msg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerCreateQuestionnaire {

    @EJB
    QuestionnaireDAOImpl questionnaireServico;
    
    @EJB
    CategoryDAOImpl        categoryService;
   
    private Questionnaire  questionnaire;
    private Category       category;
    private List<Category> categorys;
    private List<Category> selectCategory;
    private String         title;
    private Category       editCategory;
    
    public ManagerCreateQuestionnaire() {
    }

    @PostConstruct
    public void init() {
        this.questionnaire = new Questionnaire();
        categorys          = new ArrayList<Category>();
        category           = new Category();
        selectCategory     = new ArrayList<Category>();
    }
    
    public List<Category> lstCategorys(){
        return categorys = categoryService.findAll();
    }
    
    public void save() {
        if (categorys.isEmpty()) {
            Msg.messagemInfo("Adicione as Categorias para o Questionário ");
        } else {
           
            this.questionnaire.setSituation(false);
            this.questionnaire.setState(true);
            this.questionnaire.setCategorys(selectCategory);
            questionnaireServico.save(this.questionnaire);
            //Msg.messagemInfo("Questionário Salvo Com Sucesso ");
            redirect("viewQuestionnaire.xhtml");
        }
    }
     
    public void delete(Questionnaire q){
        this.questionnaire = q;
        this.questionnaire.setState(false);
        questionnaireServico.update(questionnaire);
    }
    
    
    // Resgata o Questionário
    public void resgate(Questionnaire q){
        this.questionnaire = q;
        this.questionnaire.setState(true);
        questionnaireServico.update(questionnaire);
    }

    // Retorna Issues e Joga para o TypeIssues o seu tipo e redireciona a página;
    public Issues returnTypeIssues(Issues issues, TypeIssues type) {
        issues.setTypeIssues(type);
        return issues;
    }

    // Novas Páginas
    public void newCategory() {
        if (title.equals("")) {
            Msg.messagemInfo("Adicione um Título para o Questionário");
        } else {
            System.err.println(this.questionnaire);
            categorys = new ArrayList<Category>();
            redirect("createCategory.xhtml");
        }
    }

    public void newQuestionnaire() {
        questionnaire = new Questionnaire();
        redirect("createQuestionnaire.xhtml");
    }

    public void addCategorys() {
       
        categorys.add(category);
        category = new Category();
        System.out.println(categorys);
    }
      
    public void handleSelect(SelectEvent event) {
        Category catSelec = (Category) event.getObject();
        this.category = categoryService.find(catSelec.getId());
        
        System.err.println("HANDLESELECT Categoria"+category);

    }

      public List<Questionnaire> completeQuestionnaire(String query) {  
        List<Questionnaire> bd = new ArrayList<Questionnaire>();  
        
        Questionnaire q = new Questionnaire();
        q.setTitulo(query);
        bd = questionnaireServico.autoCompleteQuestionnaire(q);
        return bd;  
    } 

    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            questionnaire = new Questionnaire();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateQuestionnaire.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void redirect(String string, Questionnaire q) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            this.questionnaire = q;
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateQuestionnaire.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Questionnaire getQuestionnaire() {
        return questionnaire;
    }

    public void setQuestionnaire(Questionnaire questionnaire) {
        this.questionnaire = questionnaire;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Category> getCategorys() {
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public List<Category> getSelectCategory() {
        return selectCategory;
    }

    public void setSelectCategory(List<Category> selectCategory) {
        this.selectCategory = selectCategory;
    }

      
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Category getEditCategory() {
        return editCategory;
    }

    public void setEditCategory(Category editCategory) {
        this.editCategory = editCategory;
    }
    
    
}
